\newpage
\section{Derivatives and differential equations}


\subsection{Derivatives}
A derivative of a function $f$ with respect to an independent variable $x$ is the infinitesimal change of the function $f$ with respect to the variable $x$. The derivative is sometimes defined as the rate of change of the function $f$ is the independent variable is time (e.g $f(t)$). If any other independent variable is taken, then the derivative is known as the evolution of the function $f$ with respect to that independent variable.

Derivatives can be represented with the Leibniz notation:

\begin{equation*}
     {dy \over dt}= {dy(t) \over dt} =  {df \over dt}= {df(t) \over dt} 
\end{equation*}

or with the Newtom notation:

\begin{equation*}
    y' = {dy(t) \over dt}
\end{equation*}

alternatively, overdots can represent differentiation with respect to $t$ as here:

\begin{equation*}
    \dot{x} = {dy(t) \over dt}
\end{equation*}

\subsection{Differential equations}
A differential equation (DE) is an equation involving derivatives. DE involves an unknown function (i.e the dependent variable) and its derivatives with respect to one or more indepedent variables. For example:

\begin{equation*}
{d^2y \over dt^2} + 2{dy \over dt} - 3y  = 0
\end{equation*}

contains derivatives of the function $y$ with respect to $t$. To denote that the $y$ is a function of $t$, the differential equation can be also written as follows:

\begin{equation*}
{d^2y(t) \over dt^2} + 2{dy(t) \over dt} - 3y(t)  = 0
\end{equation*}

\subsection{Solution to a differential equation}
To find the solution to a differential equation simply implies to find the function that fullfits the equation. Once we plug the function and its derivatives in the differential equation, the equation will hold for any value of the independent variable. This means that we do not know what the function is, but only how it changes.

For example, the following differential equation:
\begin{equation}
{dy(t) \over dt} = -k \cdot y(t)
\end{equation}

has the solution 
\begin{equation*}
y(t) = C_0 \cdot e^{k \cdot t}
\end{equation*}

Note that $C_0$ is an arbitrary constant, so not only one function, but a whole familiy of functions will fullfit the differential equation. This is therefore called the \emph{general solution}. Generally, if we set some initial and/or boundary conditions, we can solve the equation to give us an unique function. Under this conditions, the solution is usually only one function. 

\subsection{Solving first order ODEs: separation of variables}

Sometimes is not possible to solve a differential equation analytically, and for that reason we use numerical methods. However, in some cases we can solve the differential equation with different methods. One of them is the separation of variables. It works as follows:

\begin{equation*}
{dy \over dt} = \frac{g(t)}{h(y)} 
\end{equation*}

If we now reorganize $dt$ and $dy$

\begin{equation*}
h(y) \, dy = g(x)\, dt 
\end{equation*}

and integrate both sides:

\begin{equation*}
\int h(y) \, dy = \int g(t) \, dt + C
\end{equation*}

A special subclass of separable ODEs is autonomous ODEs. They have the form:

\begin{equation*}
{dy \over dt} = f(y)
\end{equation*}

where the derivative (slope) only dependends on the dependent variable (unknown function).
\subsubsection{An example}
If we want to find the function x(t) that satisfy the following differential equation:
\begin{equation*}
{dx \over dt} = \frac{\cos{t}}{2x} 
\end{equation*}

\begin{equation*}
\int 2x \, dx =\int \cos{t} \, dt 
\end{equation*}

\begin{equation*}
x^2  = \sin{t} + c 
\end{equation*}

\begin{equation*}
x(t) = \sqrt{\sin{t} + c }
\end{equation*}

To know the value of the constant $c$ we have to know at some time $t$ the value of $x(t)$. For example, assume that $x(t=0) = x_0$

\begin{equation*}
x(0) = x_0 =  \sqrt{\sin{0} + c }
\end{equation*}
then
\begin{equation*}
x_0 =  \sqrt{c}, \text{ therefore } c = x_0^2
\end{equation*}

Finally, the solution
\begin{equation*}
x(t) = \sqrt{\sin{t} + x_0^2 }
\end{equation*}

In Sage:

\begin{sageblock}
# independent variable
t=var('t')

# the function
x = function('x',t)

# define ODE
myODE = diff(x,t) - cos(t)/(2*x)==0

# solve ODE
desolve(de=myODE, ivar=t, dvar=x)

# initial conditions
x0 = var('x0')

desolve(de=myODE, ivar=t, dvar=x, ics=[0, x0])
\end{sageblock}

\subsection{Second order ODEs}
Consider the follwoing second order ODE:

\begin{equation*}
{d^2x(t) \over dt^2} + x(t) = 0 
\end{equation*}

It represents the displacement of an undamped spring with a unit of mass attached to it. In general, the number of initial conditions should match the order of the differential equation. We choose $x(0)=0$ and $\displaystyle{{dx \over dt}(0)=1}$

\begin{sageblock}
# independent variable
t=var('t')

# the function
x = function('x',t)

# define ODE
myODE = diff(x,t,2) +x ==0

# solve ODE
mysol(t) = desolve(de=myODE, ivar=t, dvar=x, ics=[0,0,1])

\end{sageblock}
\sageplot[width=8cm]{
plot(mysol, [t, 0, 2*pi], fontsize=16)
}


Another example:
\begin{equation*}
{d^2x(t) \over dt^2} + 4{dx(t) \over dt} + 4x(t) = 0 
\end{equation*}

this represent a critically damped string with a mas attached.
\begin{sageblock}
# independent variable
t=var('t')

# the function
x = function('x',t)

# define ODE
myODE = diff(x,t,2) +4*diff(x,t) + 4*x ==0

# solve ODE
mysol(t) = desolve(de=myODE, ivar=t, dvar=x, ics=[0,0,1])

\end{sageblock}
\sageplot[width=8cm]{
plot(mysol, [t, 0, 2*pi], fontsize=16)
}
